const handler = (timeout = [], mark) => {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      resolve(mark);
    }, timeout);
  }).catch(e => ("Erorr"))
};

const urls = [
  [3000, 0],
  [2000, 1],
  [8000, 2],
  [10000, 3],
  [5000, 4],
  [9000, 5],
  [2000, 6],
  [3000, 7],
  [7000, 8],
  [1000, 9],
  [6000, 10],
  [4000, 11],
  [5000, 12],
  [2000, 13],
  [4000, 14],
];

// for (const iterator of urls) {
//     handler(...iterator).then(mark => {
//         console.log(`>>>请求序列:${mark}号请求响应>>>`);
//     });
// }

const blockFn = (limit = 3) => {
  let count = 0;
  return async function dCall (timeout = [], mark) {
    count++;
    console.log(`=== 并发数:${count} ===`);
    let no = await handler(timeout, mark);
    count--;
    console.log(`>>>请求序列:${no}  应答`);
    if (urls.length) {
      dCall(...urls.shift());
    }
    if (0 == count) {
      console.timeEnd("######执行完毕######");
    }
  }
}

const sum = 5;

const limitLoad = blockFn(sum);

console.time("######执行完毕######");

for (let index = 0; index < sum; index++) {
  limitLoad(...urls.shift());
}



